import '@/js/common/filter.js'
import instance from '@/api/index.js'
import pagination from '@/components/common/pageComponent'

export default {
  name: "OrderList",
  data() {
    return {
      orderList: [],//订单列表
      orderStatus: new Map(),//订单状态
      platformStatus: new Map(),//平台状态
      conditions: {
        channel:0,
        account:'',
        platformOrderId: '',
        end: '',
        begin: '',
        page:1,
      },
      platformChannelsBtns: [],//平台渠道
      shopListBtn: [],
      pages: {
        total: 0,
        current: 1,
        display: 10,
      },
    }
  },
/*  components: {
    pagination
  },*/
  methods: {
    //清除订单列表查询条件
    clearCondtionParam: function () {
      let vm = this;
      vm.conditions.account = '';
      vm.conditions.channel = 0;
      vm.conditions.platformOrderId = null;
      vm.conditions.end = null;
      vm.conditions.begin = null;
      vm.getOrderList();

    },
    //分页
    pagechange: function (currentPage) {
      this.conditions.page = currentPage;
      this.getOrderList();
    },

    platformChannels: function () {
      let vm = this;
      instance({
        method: 'post',
        url: 'v1/author/getAuthorShopList',
        data: {
          id: vm.conditions.channel
        }
      }).then(function (data) {
        if (data.data.success) {
          vm.shopListBtn = data.data.data;
        }
      })
    },

    //获取平台列表
    getPlatform: function () {
      let vm = this;
      instance({
        method: 'post',
        url: '/v1/author/getPlatFrom'
      }).then(function (data) {
        if (data.data.success) {
          vm.platformChannelsBtns = data.data.data;
        }
      })
    },

    //获取店铺账号列表
    getShopList: function () {
      let vm = this;
      var id = null;
      if (vm.conditions.channel) {
        id = vm.conditions.channel;
      }
      instance({
        method: 'post',
        url: 'v1/author/getAuthorShopList',
        data: {
          id: id
        }
      }).then(function (data) {
        if (data.data.success) {
          vm.shopListBtn = data.data.data;
        }
      })
    },
    getPlatFormName:function(id){
      let vm = this;
      for(let i = 0; i< vm.platformChannelsBtns.length; i++){
        let item = vm.platformChannelsBtns[i];
        if(item.id == id){
          return item.platformName;
        }
      }
    },
    getOrderStatus:function(id){
      let vm = this;
      return vm.orderStatus.get(id);
    },
    //获取订单列表
    getOrderList: function () {
      let vm = this;
      instance({
        method: 'post',
        url: 'v1/order/get/u8/list',
        data:vm.conditions
      }).then(function (data) {
        if (data.data.success) {
          if(!data.data.data){
            vm.orderList = [];
          }else {
            vm.orderList = data.data.data.list;
          }
          vm.pages.total = data.data.data.total
          vm.pages.current = data.data.data.pageNum;
        } else {
          vm.alertMessage(false);
        }
      })
    },

    //获取店铺名字
    getStoreNameById: function (id) {
      let vm = this;
      for (let i = 0; i < vm.shopListBtn.length; i++) {
        if (vm.shopListBtn[i].id == id) {
          return vm.shopListBtn[i].name;
        }
      }
    },
    //导出到U8
    u8OrderDownload: function () {
      let vm = this;
      let param = {};
      if (!vm.conditions.begin) {
        vm.$alert('请选择订单开始时间！', '提示', {
          confirmButtonText: '确定',
        });
        return;
      }
      if (!vm.conditions.end) {
        vm.$alert('请选择订单结束时间！', '提示', {
          confirmButtonText: '确定',
        });
        return;
      }
      param.channel = vm.conditions.channel;
      param.begin = vm.conditions.begin;
      param.end = vm.conditions.end;
      param.orderId = vm.conditions.platformOrderId;
      instance({
        method: 'post',
        url: '/v1/order/export/u8',
        data: param,
        responseType: 'blob'
      }).then(function (data) {
        console.log(data)
        if (!data.data) {
          vm.$alert('下载失败！', '提示', {
            confirmButtonText: '确定',
          });
        } else {
          vm.download(data);
        }
      });
    },
    // 下载文件
    download(data) {
      if (!data) {
        vm.$alert('下载失败！', '提示', {
          confirmButtonText: '确定',
        });
        return;
      }
      if (window.navigator.msSaveBlob) {
        // for ie 10 and later
        try {
          let blob = new Blob([data.data], {type: "application/vnd.ms-excel"});
          // let blob = new Blob([data.data], {type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"});
          window.navigator.msSaveBlob(blob, 'u8订单.xlsx');
        }
        catch (e) {
          console.log(e);
        }
      } else {
        let blob = new Blob([data.data], {type: "application/vnd.ms-excel"});
        // let blob = new Blob([data.data], {type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"});
        let url = URL.createObjectURL(blob);
        let link = document.createElement('a');
        link.style.display = 'none';
        link.href = url;
        link.setAttribute('download', 'u8订单');
        document.body.appendChild(link);
        link.click();
        URL.revokeObjectURL(url);
      }
    },
    //初始化页面基础数据
    initBasicData: function () {
      //初始化订单状态
      this.orderStatus.set(1, "待确认");
      this.orderStatus.set(2, "待发货");
      this.orderStatus.set(3, "配货中");
      this.orderStatus.set(4, "已取消");
      this.orderStatus.set(5, "已发货");
    },
  }
  ,
  created() {
    this.getShopList();
    this.initBasicData();
    this.getPlatform();
  }
  ,
  components: {
    pagination
  }
}
